package org.dakuo.sgfyTest;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestRex {
	
	public static void main(String[] arg) throws Exception{
		String FilePath = "sanguoIncityXmlResponse.xml";
//		String FilePath = "update.xml";
//		String FilePath = "htmlResponse.html";

        FileReader fr = null;
        BufferedReader reader = null;
		
		try {
			fr = new FileReader(FilePath);
			reader = new BufferedReader(fr);
			
			Pattern p = Pattern.compile("sgtitle=\"\\s+(\\W+)(\\s+)Lv.(\\d+)(\\s+)\".*(MM_xmlLoad\\(')(\\w+\\.\\w+\\&\\w+=\\d+)(.*/>$)", Pattern.DOTALL);
			
			Pattern pEmpty = Pattern.compile("sgtitle=\"(\\s)+(空地)(\\s+).*(MM_xmlLoad\\(')(\\w+\\.\\w+\\&\\w+=\\d+)(.*/>$)", Pattern.DOTALL);
			Pattern pResource = Pattern.compile("<reso\\sid=\"(\\w+)\"\\snow=\"(\\d+(.\\d+)*)\"\\smax=\"(\\d+(.\\d+)*)\"\\sspeed=\"(\\d+(.\\d+)*)\"\\sincrease=\"(\\d+(.\\d+)*)\"\\s(buff=\"(\\d+(.\\d+)*)\")*");
			Pattern pUpdate = Pattern
			.compile(
					".*(MM_xmlLoad\\(')(\\w+\\.\\w+\\&\\w+=\\d+)(.*开始升级</a>$)",
					Pattern.DOTALL);
			

			Pattern pVillageInfo = Pattern.compile("<village\\sid=\"(\\d+)\"\\sname=\"(\\W+)\"\\sx=\"(\\d+)\"\\sy=\"(\\d+)\"\\sstate=\"(\\W+)\"\\sismain=\"(\\d)\"\\sisstatevillage=\"(\\d)\"\\s/>$");
			
			Pattern pUpdateDuration = Pattern.compile(
					"sgtitle=\"所需时间\"\\s*/>\\s*(\\d+):(\\d+):(\\d+).*</p>$",
					Pattern.DOTALL);
			Matcher m;
			Matcher mEmpty;
			Matcher mResource;
			Matcher mUpdate;
			Matcher mVillageInfo;
			Matcher mUpdateDuration;
			
			String line =reader.readLine();
			while (line!= null) {
			    m = p.matcher(line);
			    mEmpty = pEmpty.matcher(line);
			    mResource = pResource.matcher(line);
			    mUpdate = pUpdate.matcher(line);
			    mVillageInfo = pVillageInfo.matcher(line);
			    mUpdateDuration = pUpdateDuration.matcher(line);
			    if (m.find()) {
			    	System.out.println(m.group(1).substring(0, m.group(1).length()-1)+"__"+m.group(3)+"__"+m.group(6));
			    } 
			    else if(mEmpty.find()){
//			    	System.out.println( mEmpty.group(2)+"__"+mEmpty.group(5));
			    }
			    else if(mResource.find())
			    {
//			    	System.out.println(mResource.group(1)+"__"+mResource.group(2)+"__"+mResource.group(4)+"__"+mResource.group(6)+"__"+mResource.group(8)+"__"+mResource.group(11));
			    }
			    else if(mUpdate.find())
			    {
//			    	System.out.println(mUpdate.group(2));
			    }
			    else if(mVillageInfo.find()){
//			    	System.out.println(mVillageInfo.group(1)+"__"+mVillageInfo.group(2)+"__"+mVillageInfo.group(3)+"__"+mVillageInfo.group(4)+"__"+mVillageInfo.group(5)+"__"+mVillageInfo.group(6));
			    }
			    else if(mUpdateDuration.find()){
			    	int hour = Integer.parseInt(mUpdateDuration.group(1));
					int min = Integer.parseInt(mUpdateDuration.group(2));
					int second = Integer.parseInt(mUpdateDuration.group(3));
					double updateDuration = hour*3600*1000+min*60*1000+second*1000;
					System.out.println(updateDuration);
			    }
			    line = reader.readLine();
			}
			System.out.println("sanguo login 7th get done");

		} catch (IOException ex) {

			// In case of an IOException the connection will be released
			// back to the connection manager automatically
			throw ex;

		} catch (RuntimeException ex) {

			// In case of an unexpected exception you may want to abort
			// the HTTP request in order to shut down the underlying
			// connection and release it back to the connection manager.
			throw ex;

		} finally {

			// Closing the input stream will trigger connection release
			reader.close();

		}
	}
}
